package controllers;

import play.*;
import play.mvc.*;

import java.text.SimpleDateFormat;
import java.util.*;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.joda.time.DateTime;
import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.sun.org.apache.xerces.internal.impl.xpath.regex.ParseException;

import models.*;

public class Application extends Controller {

	public static void index(){
		List<Imovel> imovelFalso = Imovel.find("byPublicado", false).fetch();
		//String data = (new java.text.SimpleDateFormat("dd/MM/yyyy").format(new java.util.Date(System.currentTimeMillis())));;
		Calendar h = Calendar.getInstance();
		for (Imovel imovel : imovelFalso) {
			if(imovel.dataPublicacao.before(h.getTime())){
				imovel.publicado = true;
				imovel.save();
			}
		}
		List<Imovel> imovels = Imovel.find("Pago = ? order by datapublicacao desc", true).fetch();
		render(imovels);
	}
	
	public static void sincronizar() throws DOMException, ParseException, java.text.ParseException{
    	Http.Request request = new Http.Request();
    	request = request.current();
    	  
    	DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    	Document document = null;
    	try{
    		DocumentBuilder builder = factory.newDocumentBuilder();
    		document = builder.parse(request.body);
    	}catch (Exception e){
    		Logger.error(e.getMessage());
    		
    	}	
  
    	NodeList colecao = document.getElementsByTagName("Anuncio");
    	System.out.print(colecao.getLength());
    	SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");  
    	ArrayList<Anuncio> anuncios = new ArrayList<Anuncio>();
    	for (int i=0; i <= colecao.getLength()-1; i++){
    		System.out.print("Laco "+i);
    		Node fa = colecao.item(i);
    		if(fa.getNodeType() == Node.ELEMENT_NODE){

                Element faE = (Element)fa;
                
                NodeList fi = faE.getElementsByTagName("id");
                Element fiE = (Element)fi.item(0);
                NodeList textFNList = fiE.getChildNodes();
                long id = Integer.parseInt(textFNList.item(0).getNodeValue().trim());
                
                
                NodeList fnv = faE.getElementsByTagName("nVizualizacoes");
                Element fnvE = (Element)fnv.item(0);
                NodeList textFNVList = fnvE.getChildNodes();
                int nVizualizacoes = Integer.parseInt(textFNVList.item(0).getNodeValue().trim());
                
                NodeList fqA = faE.getElementsByTagName("qtdAreasServico");
                Element fqAE = (Element)fqA.item(0);
                NodeList textFqAList = fqAE.getChildNodes();
                int qtdAreasServico = Integer.parseInt(textFqAList.item(0).getNodeValue().trim());
                
                NodeList fqB= faE.getElementsByTagName("qtdBanheiros");
                Element fqBE = (Element)fqB.item(0);
                NodeList textFqBList = fqBE.getChildNodes();
                int qtdBanheiros = Integer.parseInt(textFqBList.item(0).getNodeValue().trim());
                
                NodeList fqG = faE.getElementsByTagName("qtdGaragem");
                Element fqGE = (Element)fqG.item(0);
                NodeList textFqGList = fqGE.getChildNodes();
                int qtdGaragem = Integer.parseInt(textFqGList.item(0).getNodeValue().trim());
                
                NodeList fqQ = faE.getElementsByTagName("qtdQuartos");
                Element fqQE = (Element)fqQ.item(0);
                NodeList textFqQList = fqQE.getChildNodes();
                int qtdQuartos = Integer.parseInt(textFqQList.item(0).getNodeValue().trim());
                
                NodeList fqSa = faE.getElementsByTagName("qtdSalas");
                Element fqSaE = (Element)fqSa.item(0);
                NodeList textFqSaList = fqSaE.getChildNodes();
                int qtdSalas = Integer.parseInt(textFqSaList.item(0).getNodeValue().trim());
                
                NodeList fqS = faE.getElementsByTagName("qtdSuites");
                Element fqSE = (Element)fqS.item(0);
                NodeList textFqSList = fqSE.getChildNodes();
                int qtdSuites = Integer.parseInt(textFqSList.item(0).getNodeValue().trim());
                
                NodeList fat = faE.getElementsByTagName("areaTotal");
                Element fatE = (Element)fat.item(0);
                NodeList textFATList = fatE.getChildNodes();
                Double areaTotal =  Double.parseDouble(textFATList.item(0).getNodeValue().trim());

                NodeList fcom = faE.getElementsByTagName("comprimento");
                Element fcomE = (Element)fcom.item(0);
                NodeList textFCOMList = fcomE.getChildNodes();
                Double comprimento =  Double.parseDouble(textFCOMList.item(0).getNodeValue().trim());

                NodeList flar = faE.getElementsByTagName("largura");
                Element flarE = (Element)flar.item(0);
                NodeList textFLARList = flarE.getChildNodes();
                Double largura =  Double.parseDouble(textFLARList.item(0).getNodeValue().trim());

                NodeList fpre = faE.getElementsByTagName("preco");
                Element fpreE = (Element)fpre.item(0);
                NodeList textFPREList = fpreE.getChildNodes();
                Double preco =  Double.parseDouble(textFPREList.item(0).getNodeValue().trim());
                
                
                NodeList fdtA = faE.getElementsByTagName("dtAtualizacao");
                Element fdtAE = (Element)fdtA.item(0);
                NodeList textFdtAList = fdtAE.getChildNodes();
                Date dtAtualizacao =  (Date)(format.parse(textFdtAList.item(0).getNodeValue().trim()));
                
                NodeList fdtC = faE.getElementsByTagName("dtCadastro");
                Element fdtCE = (Element)fdtC.item(0);
                NodeList textFdtCList = fdtCE.getChildNodes();
                Date dtCadastro =  (Date)(format.parse(textFdtCList.item(0).getNodeValue().trim()));
                
                NodeList fdtFP = faE.getElementsByTagName("fimPublicacao");
                Element fdtFPE = (Element)fdtFP.item(0);
                NodeList textFdtFPList = fdtFPE.getChildNodes();
                Date fimPublicacao = (Date)(format.parse(textFdtFPList.item(0).getNodeValue().trim()));
                
                NodeList fdtIP = faE.getElementsByTagName("iniPublicacao");
                Element fdtIPE = (Element)fdtA.item(0);
                NodeList textFdtIPList = fdtAE.getChildNodes();
                Date iniPublicacao =   (Date)(format.parse(textFdtIPList.item(0).getNodeValue().trim()));
                
                NodeList fn = faE.getElementsByTagName("nota");
                Element fnE = (Element)fn.item(0);
                NodeList textFnList = fnE.getChildNodes();
                String nota =  textFnList.item(0).getNodeValue().trim();
                
                NodeList fs = faE.getElementsByTagName("status");
                Element fsE = (Element)fs.item(0);
                NodeList textFsList = fsE.getChildNodes();
                String status =  textFsList.item(0).getNodeValue().trim();
                
                
                NodeList ft = faE.getElementsByTagName("tags");
                Element ftE = (Element)ft.item(0);
                NodeList textFtList = ftE.getChildNodes();
                String tags =  textFtList.item(0).getNodeValue().trim();
                
                
                NodeList ftI = faE.getElementsByTagName("tipoImovel");
                Element ftIE = (Element)ftI.item(0);
                NodeList textFtIList = ftIE.getChildNodes();
                String tipoImovel =  textFtIList.item(0).getNodeValue().trim();
                
                NodeList ftt = faE.getElementsByTagName("titulo");
                Element fttE = (Element)ftt.item(0);
                NodeList textFttList = fttE.getChildNodes();
                String titulo =  textFttList.item(0).getNodeValue().trim();
                
                
                /*NodeList fwi = faE.getElementsByTagName("web_id");
                Element fwiE = (Element)fwi.item(0);
                NodeList textFwiList = fwiE.getChildNodes();
                String wi =  textFwiList.item(0).getNodeValue().trim();
                */
                NodeList fb = faE.getElementsByTagName("bairro");
                Element fbE = (Element)fb.item(0);
                NodeList textFbList = fbE.getChildNodes();
                String bairro =  textFbList.item(0).getNodeValue().trim();
                
                
                NodeList fe = faE.getElementsByTagName("estado");
                Element feE = (Element)fe.item(0);
                NodeList textFeList = feE.getChildNodes();
                String estado =  textFeList.item(0).getNodeValue().trim();
                System.out.print(estado);
                
                NodeList fcp = faE.getElementsByTagName("cep");
                Element fcpE = (Element)fcp.item(0);
                NodeList textFcpList = fcpE.getChildNodes();
                String cep =  textFcpList.item(0).getNodeValue().trim();
                
                NodeList fcd = faE.getElementsByTagName("cidade");
                Element fcdE = (Element)fcd.item(0);
                NodeList textFcdList = fcdE.getChildNodes();
                String cidade =  textFcdList.item(0).getNodeValue().trim();
                
                NodeList fcmp = faE.getElementsByTagName("complemento");
                Element fcmpE = (Element)fcmp.item(0);
                NodeList textFcmpList = fcmpE.getChildNodes();
                String complemento =  textFcmpList.item(0).getNodeValue().trim();
                
                NodeList flog = faE.getElementsByTagName("logradouro");
                Element flogE = (Element)flog.item(0);
                NodeList textFlogList = flogE.getChildNodes();
                String logradouro =  textFlogList.item(0).getNodeValue().trim();
                
                NodeList fnum = faE.getElementsByTagName("numero");
                Element fnumE = (Element)fnum.item(0);
                NodeList textFnumList = fnumE.getChildNodes();
                String numero =  textFnumList.item(0).getNodeValue().trim();
                
                Estado e = new Estado();
                e.descricao = estado;
                Cidade c = new Cidade();
                c.descricao = cidade;
                c.estado = e;
                Bairro b = new Bairro();
                b.cidade = c;
                b.descricao = bairro;
                Imovel imovel = new Imovel();
                imovel.bairro = b;
                imovel.cep = cep;
                imovel.areaTotal = areaTotal;
                imovel.comprimento = comprimento;
                
                imovel.descricao = titulo;
                imovel.largura = largura;
                imovel.quartos = qtdQuartos;
                imovel.status = true;
                imovel.suites = qtdSuites;
                TipoImovel ti = new TipoImovel();
                ti.descricao = tipoImovel;
                imovel.tipoImovel = ti;
                imovel.usuario = Usuario.find("byEmail", Security.connected()).first();;
                imovel.vagasGaragem = qtdGaragem;
                imovel.valor = preco;
                
                
                Anuncio a = new Anuncio(fimPublicacao, iniPublicacao, imovel);
               
                
                
               	anuncios.add(a);
    		}
    	}
    	System.out.print("anuncio taba"+anuncios.size());
    	
    	if (anuncios.size() > 0)
    		Anuncio.sincronizar(anuncios);
    }

}